//
//  SetUpTimeViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/14.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "SetUpTimeViewController.h"
#import "JobSetUpViewController.h"
#import "SetUpTimeModel.h"
#import "FreeDatesModel.h"
#import "FreeTimeModel.h"

static NSString *cellPath = @"setuptimecell";
static NSString *cellTimePath = @"timecell";
@interface SetUpTimeViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
{
    UIButton *unificationBtn;
}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UITableView *timeTableview;
@property (nonatomic, strong)UIView *blackView;
@property (nonatomic, strong)NSMutableDictionary *allDayDic;
@property (nonatomic, strong)NSMutableDictionary *dayNewDic;
@property (nonatomic, strong)NSArray *resultArray;
@property (nonatomic, strong)NSMutableArray *allTimeArray;
@property (nonatomic, strong)NSMutableArray *closeTimeAry;
@property (nonatomic, assign)BOOL isUnification;
@property (nonatomic, strong)NSMutableDictionary *allSetUpDic;
@property (nonatomic, strong)NSString *monthString;
@property (nonatomic, strong)NSString *dayString;
@property (nonatomic, strong)NSArray *allHaveTimeAry;

@end

@implementation SetUpTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _closeTimeAry = [NSMutableArray arrayWithCapacity:0];
    _allTimeArray = [NSMutableArray arrayWithCapacity:0];
    _allSetUpDic = [NSMutableDictionary dictionaryWithCapacity:0];
    _dayNewDic = [NSMutableDictionary dictionaryWithCapacity:0];
    for (int i = 0; i < 24; i++)
    {
        NSString *str = [NSString stringWithFormat:@"%02d : 00 - %02d : 00",i, i+1];
        if (i == 23)
        {
            str = [str stringByReplacingOccurrencesOfString:@"24" withString:@"00"];
        }
        [_allTimeArray addObject:str];
    }
    [self creatUI];
    [self requestWorkerFreeDateParmers];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_allDayDic && ![_allDayDic isKindOfClass:[NSNull class]]) {
        unificationBtn.backgroundColor = ThemeColor;
        unificationBtn.enabled = YES;
    } else {
        unificationBtn.backgroundColor = [UIColor lightGrayColor];
        unificationBtn.enabled = NO;
    }
}

- (void)creatUI
{
    self.navigationItem.title = @"产线设置";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tianjiatime"] style:UIBarButtonItemStylePlain target:self action:@selector(addTimeAction)];
    UIBarButtonItem *rightBarBtn1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shanchu(2)"] style:UIBarButtonItemStylePlain target:self action:@selector(deleteTimeAction)];
    self.navigationItem.rightBarButtonItems = @[rightBarBtn, rightBarBtn1];
    
    if (_setupTimeType == ALLSETUPTIMETYPE) {
        self.navigationController.navigationBar.hidden = NO;
        self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SCREER_HeiX-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    } else {
        self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-80*displaywidth-SCREER_HeiX-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    }
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tag = 11;
    [_tableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    _tableview.backgroundColor = BacksColor;
    [self.view addSubview:_tableview];
    
    unificationBtn = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth , SCREEN_HEIGHT-60*displaywidth-SCREER_HeiX-STA_NAV_HEIGHT, SCREEN_WIDTH/2-40*displaywidth, 40*displaywidth) ImageName:nil Target:self Action:@selector(unificationAction) Title:@"一键设置" Font:15.0];
    [unificationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    unificationBtn.backgroundColor = ThemeColor;
    [unificationBtn.layer setMasksToBounds:YES];
    [unificationBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:unificationBtn];
    
    UIButton *saveBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2+20*displaywidth , SCREEN_HEIGHT-60*displaywidth-SCREER_HeiX-STA_NAV_HEIGHT, SCREEN_WIDTH/2-40*displaywidth, 40*displaywidth) ImageName:nil Target:self Action:@selector(saveTimeAction) Title:@"保存" Font:15.0];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.backgroundColor = ThemeColor;
    [saveBtn.layer setMasksToBounds:YES];
    [saveBtn.layer setCornerRadius:5*displaywidth];
    [self.view addSubview:saveBtn];
    
    self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _blackView.backgroundColor = RGBA(0, 0, 0, 0.5);
    _blackView.hidden = YES;
    [sharedDelegate.window addSubview:_blackView];
    UITapGestureRecognizer *tap11 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ggViewPressed)];
    tap11.delegate = self;
    [_blackView addGestureRecognizer:tap11];
    self.timeTableview = [[UITableView alloc] initWithFrame:CGRectMake(50*displaywidth, SCREEN_HEIGHT/2-180*displaywidth, SCREEN_WIDTH-100*displaywidth, 320*displaywidth) style:UITableViewStylePlain];
    _timeTableview.delegate = self;
    _timeTableview.dataSource = self;
    _timeTableview.tag = 22;
    _timeTableview.backgroundColor = [UIColor whiteColor];
    _timeTableview.layer.cornerRadius = 10;
    _timeTableview.layer.masksToBounds = YES;
    [_blackView addSubview:_timeTableview];
}

- (void)reloadDataTheTimes
{
    [_dayNewDic removeAllObjects];
    for (NSString *key in _allDayDic)
    {
        NSArray *dayAry = _allDayDic[key];
        if (dayAry.count > 0)
        {
            NSComparator finderSort = ^(id string1,id string2){
                if ([[string1 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue] < [[string2 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]) {
                    return (NSComparisonResult)NSOrderedDescending;
                }else if ([[string1 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue] > [[string2 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]){
                    return (NSComparisonResult)NSOrderedAscending;
                }
                else
                    return (NSComparisonResult)NSOrderedSame;
            };
            NSArray *sortedArray = [dayAry sortedArrayUsingComparator:finderSort];
            
            
            [_dayNewDic setObject:sortedArray forKey:key];
        }
    }
    
    NSArray *keyAry = [_dayNewDic allKeys];
    NSComparator finderSort = ^(id string1,id string2){
        
        if ([[string1 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue] < [[string2 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }else if ([[string1 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue] > [[string2 stringByReplacingOccurrencesOfString:@"-" withString:@""] integerValue]){
            return (NSComparisonResult)NSOrderedAscending;
        }
        else
            return (NSComparisonResult)NSOrderedSame;
    };
    
    //数组排序：
    _resultArray = [keyAry sortedArrayUsingComparator:finderSort];
    if (_allDayDic && ![_allDayDic isKindOfClass:[NSNull class]]) {
        unificationBtn.backgroundColor = ThemeColor;
        unificationBtn.enabled = YES;
    } else {
        unificationBtn.backgroundColor = [UIColor lightGrayColor];
        unificationBtn.enabled = NO;
    }
    [_tableview reloadData];
}

- (void)ggViewPressed
{
    _blackView.hidden = YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:_timeTableview]) {
        return NO;
    }
    return YES;
}

//添加
- (void)addTimeAction
{
    JobSetUpViewController *vc = [[JobSetUpViewController alloc] init];
    vc.haveTimeAry = _allHaveTimeAry;
    WEAKSELF;
    vc.timeBlock = ^(NSMutableDictionary *dayDic)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf insertHaveTime:dayDic];
            
        });
        
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)deleteTimeAction {
//    NSMutableArray *dayAry = [NSMutableArray arrayWithArray:_dayNewDic[_resultArray[indexPath.section]]];
//    [_allSetUpDic[_resultArray[indexPath.section]] removeObjectForKey:dayAry[indexPath.row]];
//    [dayAry removeObjectAtIndex:indexPath.row];
//    _dayNewDic[_resultArray[indexPath.section]] = dayAry;
//    [_tableview reloadData];
    [_allSetUpDic removeAllObjects];
    [_dayNewDic removeAllObjects];
    _allHaveTimeAry = nil;
    [self requestClearPipelineFreeDateParmers];
    [_tableview reloadData];
}

- (void)insertHaveTime:(NSMutableDictionary *)dic
{
   
    if (_allHaveTimeAry.count > 0)
    {
        [_allSetUpDic removeAllObjects];
        NSMutableDictionary *newBigDic = [NSMutableDictionary dictionaryWithCapacity:0];
        [newBigDic addEntriesFromDictionary:dic];
        for (SetUpTimeModel *timeModel in _allHaveTimeAry)
        {
            NSString *monthStr = [NSString stringWithFormat:@"%02ld",[timeModel.month integerValue]];
            NSString *yearMonth = [NSString stringWithFormat:@"%@-%@",timeModel.year,monthStr];
            NSArray *dayAry = timeModel.freeDates;
            NSMutableDictionary *setUpDic = [NSMutableDictionary dictionaryWithCapacity:0];
            for (FreeDatesModel *dateModel in dayAry)
            {
                NSArray *sectAry = dateModel.freeTimes;
                NSMutableArray *sectonTimeArray = [NSMutableArray arrayWithCapacity:0];
                for (FreeTimeModel *hour in sectAry)
                {
                    [sectonTimeArray addObject:[NSString stringWithFormat:@"%@",hour.section]];
                }
                [setUpDic setObject:sectonTimeArray forKey:dateModel.freeDate];
            }
            [_allSetUpDic setObject:setUpDic forKey:yearMonth];
            if ([dic.allKeys containsObject:yearMonth])
            {
                
                NSArray *closeArray = dic[yearMonth];
                NSMutableArray *oneArray = [NSMutableArray arrayWithCapacity:0];
                [oneArray addObjectsFromArray:closeArray];
                for (FreeDatesModel *dateModel in dayAry)
                {
                    if (![oneArray containsObject:dateModel.freeDate])
                    {
                        [oneArray addObject:dateModel.freeDate];
                    }
                }
                [newBigDic setObject:oneArray forKey:yearMonth];
            }
            else
            {
                NSMutableArray *oneArray = [NSMutableArray arrayWithCapacity:0];
                for (FreeDatesModel *dateModel in dayAry)
                {
                    [oneArray addObject:dateModel.freeDate];
                    
                }
                [newBigDic setObject:oneArray forKey:yearMonth];
            }
        }
        _allDayDic = newBigDic;
    }
    else
    {
        _allDayDic = dic;
    }
    [self reloadDataTheTimes];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView.tag == 11)
    {

        return _dayNewDic.allKeys.count;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 11)
    {

        NSArray *dayAry = _dayNewDic[_resultArray[section]];
        return dayAry.count;
    }
    return _allTimeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        NSString *cellPathStr = cellPath;
        if (tableView.tag == 22)
        {
            cellPathStr = cellTimePath;
        }
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPathStr];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 11)
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = NormalFontWithSize(14.0+fontScale);
        cell.textLabel.textColor = RGB(77, 77, 77);

        NSArray *dayAry = _dayNewDic[_resultArray[indexPath.section]];
        cell.textLabel.text = dayAry[indexPath.row];
        if ([[_allSetUpDic allKeys] containsObject:_resultArray[indexPath.section]])
        {
            NSDictionary *monthdic = [_allSetUpDic objectForKey:_resultArray[indexPath.section]];
            if ([monthdic.allKeys containsObject:dayAry[indexPath.row]])
            {
                NSArray *numAry = [monthdic objectForKey:dayAry[indexPath.row]];
                if (numAry.count > 0)
                {
                    UILabel *numTimeLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-100*displaywidth, 5*displaywidth, 70*displaywidth, 40*displaywidth) Font:12.0 Text:[NSString stringWithFormat:@"%ld 小时",numAry.count] Color:RGB(128, 128, 128)];
                    numTimeLab.textAlignment = NSTextAlignmentRight;
                    [cell.contentView addSubview:numTimeLab];
                }
               
            }
        }

    }
    else
    {
        for (id subView in cell.contentView.subviews) {
            [subView removeFromSuperview];
        }
        cell.textLabel.text = _allTimeArray[indexPath.row];
        UIImageView *imgView = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-135*displaywidth, 10*displaywidth, 20*displaywidth, 20*displaywidth) ImageName:@"xuankuang-1"];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.hidden = NO;
        if ([_closeTimeAry containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]])
        {
            imgView.image = [UIImage imageNamed:@"gouxuan1"];
        }
        [cell.contentView addSubview:imgView];
    
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 22)
    {
        if ([_closeTimeAry containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]])
        {
            [_closeTimeAry removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
        }
        else
        {
            [_closeTimeAry addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
        }
        [UIView performWithoutAnimation:^{
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }
    else
    {
        _isUnification = NO;
        _blackView.hidden = NO;
        [_closeTimeAry removeAllObjects];
        NSArray *dayAry = _dayNewDic[_resultArray[indexPath.section]];
        _monthString = _resultArray[indexPath.section];
        _dayString = dayAry[indexPath.row];
        if ([[_allSetUpDic allKeys] containsObject:_resultArray[indexPath.section]])
        {
            NSDictionary *monthdic = [_allSetUpDic objectForKey:_resultArray[indexPath.section]];
            if ([monthdic.allKeys containsObject:dayAry[indexPath.row]])
            {
                NSArray *numAry = [monthdic objectForKey:dayAry[indexPath.row]];
                [_closeTimeAry addObjectsFromArray:numAry];
            }
        }
        [_timeTableview reloadData];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myView = [[UIView alloc] init];
    if (tableView.tag == 11)
    {
        myView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 30*displaywidth);
        myView.backgroundColor = BacksColor;
        UILabel *monthLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 0, SCREEN_WIDTH, 29*displaywidth) Font:14.0 Text:_resultArray[section] Color:RGB(77, 77, 77)];
        monthLab.backgroundColor = BacksColor;
        [myView addSubview:monthLab];
    }
    else
    {
        myView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40*displaywidth);
        myView.backgroundColor = BacksColor;
        UILabel *monthLab = [ZCControl createLabelWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100*displaywidth, 39*displaywidth) Font:14.0 Text:@"选择空闲时间" Color:RGB(77, 77, 77)];
        monthLab.backgroundColor = BacksColor;
        monthLab.textAlignment = NSTextAlignmentCenter;
        [myView addSubview:monthLab];
    }
    return myView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *myView = [[UIView alloc] init];
    if (tableView.tag == 22)
    {
        myView.frame = CGRectMake(0, 0, SCREEN_WIDTH-100*displaywidth, 40*displaywidth);
        myView.backgroundColor = BacksColor;
        CGFloat W = SCREEN_WIDTH-100*displaywidth;
        UIButton *cancelBtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, W/2, 40*displaywidth) ImageName:nil Target:self Action:@selector(ggViewPressed) Title:@"取消" Font:14.0];
        [cancelBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [myView addSubview:cancelBtn];
       
        UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(W/2, 0, W/2, 40*displaywidth) ImageName:nil Target:self Action:@selector(soureBtnAction) Title:@"确定" Font:14.0];
        [soureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [myView addSubview:soureBtn];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(W/2-0.5, 0, 1*displaywidth, 40*displaywidth)];
        lineView.backgroundColor = RGB(204, 204, 204);
        [myView addSubview:lineView];
    }
    return myView;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"点击了删除");

        NSMutableArray *dayAry = [NSMutableArray arrayWithArray:_dayNewDic[_resultArray[indexPath.section]]];
        [_allSetUpDic[_resultArray[indexPath.section]] removeObjectForKey:dayAry[indexPath.row]];
        [dayAry removeObjectAtIndex:indexPath.row];
        if (dayAry.count>0) {
            _dayNewDic[_resultArray[indexPath.section]] = dayAry;
        } else {
            [_dayNewDic removeObjectForKey:_resultArray[indexPath.section]];
            [_allSetUpDic removeObjectForKey:_resultArray[indexPath.section]];
            if (!_allSetUpDic || [_allSetUpDic isKindOfClass:[NSNull class]] || [_allSetUpDic isEqual:@{}]) {
                [self requestClearPipelineFreeDateParmers];
            }
        }
        [_tableview reloadData];
    }];

    return @[deleteAction];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    editingStyle = UITableViewCellEditingStyleDelete;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 11)
    {
        return 50*displaywidth;

    }
    return 40*displaywidth;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == 11)
    {
        return 30*displaywidth;
    }
    return 40*displaywidth;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (tableView.tag == 11)
    {
        return 0;
    }
    return 40*displaywidth;
}

//确定
- (void)soureBtnAction
{
    if (_isUnification)
    {
        [_allSetUpDic removeAllObjects];
        for (NSString *key in _dayNewDic)
        {
            NSArray *daysAry = [_dayNewDic objectForKey:key];
            NSMutableDictionary *bigDic = [NSMutableDictionary dictionaryWithCapacity:0];
            for (NSString *day in daysAry)
            {
                NSArray *timesAry = [_closeTimeAry copy];
                [bigDic setObject:timesAry forKey:day];
            }
            [_allSetUpDic setObject:bigDic forKey:key];
        }
    }
    else
    {
        if ([_allSetUpDic.allKeys containsObject:_monthString])
        {
            NSDictionary *dic = _allSetUpDic[_monthString];
            NSMutableDictionary *newDic = [NSMutableDictionary dictionaryWithCapacity:0];
            [newDic addEntriesFromDictionary:dic];
            NSArray *timesAry = [_closeTimeAry copy];
            [newDic setObject:timesAry forKey:_dayString];
            [_allSetUpDic setObject:newDic forKey:_monthString];
            
        }
        else
        {
            NSMutableDictionary *newDic = [NSMutableDictionary dictionaryWithCapacity:0];
            NSArray *timesAry = [_closeTimeAry copy];
            [newDic setObject:timesAry forKey:_dayString];
            [_allSetUpDic setObject:newDic forKey:_monthString];
        }
    }
    [self.tableview reloadData];
    [self ggViewPressed];
}

//统一设置
- (void)unificationAction
{
    _blackView.hidden = NO;
    _isUnification = YES;
    [_closeTimeAry removeAllObjects];
    [_timeTableview reloadData];
}

//保存
- (void)saveTimeAction
{
    NSMutableArray *allDayArray = [NSMutableArray arrayWithCapacity:0];
    for (NSString *monthKty in _allSetUpDic)
    {
        NSDictionary *dayDic = [_allSetUpDic objectForKey:monthKty];
        for (NSString *dayKey in dayDic)
        {
            NSArray *timeAry = [dayDic objectForKey:dayKey];
            if (timeAry.count > 0)
            {
                NSMutableArray *allTimeArray = [NSMutableArray arrayWithCapacity:0];
                for (NSString *time in timeAry)
                {
                    NSDictionary *timeDic = @{@"section":time};
                    [allTimeArray addObject:timeDic];
                }
                NSDictionary *thisDayDic = @{@"pipeline":@{@"id":_ID},
                                             @"freeDate":dayKey,
                                             @"freeTimes":allTimeArray
                                             };
                [allDayArray addObject:thisDayDic];
            }
        }
      
    }
    if (allDayArray.count == 0 && _dayNewDic.count > 0)
    {
        [LTools showMBProgressWithText:@"请设置要保存日期的空闲时间" addToView:self.view];
        return;
    }
//    [LTools showMBProgressWithText:@"未设置空闲时间的日期，将不被保存！" addToView:self.view];
    
    WEAKSELF;
    [HYBaseHandler requestSaveWorkerFreeDateParmers:allDayArray Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            [LTools showMBProgressWithText:@"保存成功" addToView:weakSelf.view];
            [weakSelf requestWorkerFreeDateParmers];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"mag"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

//我的空闲日期
- (void)requestWorkerFreeDateParmers
{
    WEAKSELF;
    [HYBaseHandler requestWorkerFreeDateParmers:@{@"pipelineId":_ID} Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            _allHaveTimeAry = [SetUpTimeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf insertHaveTime:nil];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"mag"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)requestClearPipelineFreeDateParmers {
    WEAKSELF;
    [HYBaseHandler requestClearPipelineFreeDateParmers:@{@"pipelineId":_ID} Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
//             _allHaveTimeAry = [SetUpTimeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
//             dispatch_async(dispatch_get_main_queue(), ^{
//                 [weakSelf insertHaveTime:nil];
//             });
             [weakSelf requestWorkerFreeDateParmers];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"mag"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
